var async = require("async")
module.exports = function(app) {
	return new DBRemote(app);
};
var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
}

var local = {}
var ratioList = [
	{num : 0 , ratio : 0.1},
	{num : 10 , ratio : 0.15},
	{num : 40 , ratio : 0.2}
]
//获取下属玩家列表
DBRemote.prototype.getInviterUsers = function(uid,begin,cb) {
	DBRemote.dbService.getList("game:inviteUsersList:"+uid,begin,10,function(flag,info) {
		if(!info.data.length){
			info.data = {}
			cb(true,info)
			return
		}
		DBRemote.dbService.db.hmget("game:inviteUsers:"+uid,info.data,function(err,data) {
			if(err || !data){
				info.data = {}

				return
			}
			info.data = data
			cb(true,info)
		})
	})
}
//获取总佣金
DBRemote.prototype.getAllBrokerage = function(uid,cb) {
	var info = {
		allBrokerage : 0,
		inviterRatio : 0
	}
	DBRemote.dbService.db.hget("game:allBrokerage",uid,function(err,data) {
		if(!err && data){
			info.allBrokerage = data
		}
		DBRemote.dbService.db.llen("game:inviteUsersList:"+uid,function(err,data) {
			if(err || !data){
				data = 0
			}
			var num = parseInt(data)
			for(var i = ratioList.length - 1;i >= 0;i--){
				if(num >= ratioList[i].num){
					info.inviterRatio = ratioList[i].ratio
					cb(true,info)
					return
				}
			}
			cb(true,info)
		})
	})
}
//推广人获得奖励
DBRemote.prototype.inviterBrokerage = function(uid,rmb,cb) {
	var inviterRatio = 0
	var brokerage = 0
	var inviter = 0
	async.waterfall([
		function(next) {
			//获取代理
			DBRemote.dbService.getPlayerString(uid,"inviter",function(data) {
				if(!data){
					return
				}
				inviter = data
				next()
			})
		},
		function(next) {
			//获取分成比例
			DBRemote.dbService.db.llen("game:inviteUsersList:"+inviter,function(err,data) {
				if(err || !data){
					data = 0
				}
				var num = parseInt(data)
				for(var i = ratioList.length - 1;i >= 0;i--){
					if(num >= ratioList[i].num){
						inviterRatio = ratioList[i].ratio
						break
					}
				}
				if(inviterRatio){
					next()
				}
			})
		},
		function() {
			brokerage = parseFloat((inviterRatio * rmb).toFixed(2))
			//增加佣金
			DBRemote.dbService.addItemReason(inviter,103,brokerage,"推广佣金",function(err,data){})
			DBRemote.dbService.db.hincrbyfloat("game:allBrokerage",inviter,brokerage)
			//佣金记录
			DBRemote.dbService.getSimpleInfo(uid,function(flag,player) {
				var info = {
					time : (new Date()).getTime(),
					nickname : player.nickname,
					head : player.head,
					rmb : rmb,
					inviterRatio : inviterRatio,
					brokerage : brokerage
				}
				DBRemote.dbService.db.rpush("game:brokerageRecords:"+inviter,JSON.stringify(info))
			})
			cb(true)
		}
	],function(err) {
		console.log(err)
		cb(false,err)
	})
}
//获取佣金记录
DBRemote.prototype.getBrokerageRecord = function(uid,begin,cb) {
	DBRemote.dbService.getList("game:brokerageRecords:"+uid,begin,10,cb)
}
//获取提现记录
DBRemote.prototype.getWithdrawalRecords = function(uid,begin,cb) {
	DBRemote.dbService.getList("game:withdrawalRecords:"+uid,begin,10,cb)
}